#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
#include<queue>
#include<vector>

using namespace std;
class Solution {
public:
    vector<vector<int>> floodFill(vector<vector<int>>& image, int sr, int sc, int color) {
        if (image[sr][sc] == color) return image;
        int m = image.size(), n = image[0].size();
        int start_color = image[sr][sc];
        int dr[] = { -1, 1, 0, 0 }, dc[] = { 0, 0, -1, 1 };
        vector<vector<bool>> vis(m, vector<bool>(n, 0));
        queue<pair<int, int>> q;
        q.push(make_pair(sr, sc));
        vis[sr][sc] = 1;

        while (!q.empty()) {
            auto front = q.front();
            image[front.first][front.second] = color;
            q.pop();
            for (int i = 0; i < 4; i++) {
                int nr = front.first + dr[i], nc = front.second + dc[i];
                if (nr >= 0 && nr < m && nc >= 0 && nc < n &&
                    image[nr][nc] == start_color) {
                    q.push({ nr, nc });
                    vis[nr][nc] = 1;
                }
            }
        }

        return image;
    }
};